package com.zhaopin.file;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class FileTihuan {

    /**
     * 目的是寻找在线的OSS的，没有召回量的，不是baseline 的
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

//        String path = "recommend";
        String path = "position";

        //线上有请求的
        List<String> listHaveRequestExp = FileUtils.readLines(new File("dir/file/filetihuan/" + path + "/haveRequestExp.txt"), Charset.defaultCharset());
        //在线的OSS的
        List<String> listOssExp = FileUtils.readLines(new File("dir/file/filetihuan/" + path + "/ossExp.txt"), Charset.defaultCharset());
        //baseline的
        List<String> listbaselineExp = FileUtils.readLines(new File("dir/file/filetihuan/" + path + "/baselineExp.txt"), Charset.defaultCharset());

        List<Exp> listExp = new ArrayList<>();

        //真正的baseline
        List<String> listBase = new ArrayList<>();
        for (String s : listbaselineExp) {
            String substring = s.substring(4);
            JSONObject jsonObject = JSON.parseObject(substring);
            listBase.add(jsonObject.getString("exp"));
        }


        List<String> listHave = new ArrayList<>();
        for (String s : listHaveRequestExp) {
            listHave.add(s.split(",")[0].substring(1, s.split(",")[0].length() - 1));
        }


        int i = 0;
        for (String s : listOssExp) {
            JSONObject jsonObject = JSON.parseObject(s.substring(4));
            String exp = jsonObject.getString("exp");
            String tag = jsonObject.getString("tag");


            //在线OSS的，没召回量的 //并且不是 baseline
            if (!listHave.contains(exp) && !listBase.contains(exp)) {
                Exp exp1 = new Exp(exp, tag);
                listExp.add(exp1);
            }
        }

        listExp.stream().sorted(Comparator.comparing(Exp::getExp)).forEach(
                a -> {
                    //写文件： recommend_onoss_norequest_nobaseline.txt
                    try {
                        FileUtils.write(new File("dir/file/filetihuan/" + path + "/" + path + "_onoss_norequest_nobaseline.txt"), a.getExp()+ ":" + a.getTag(), Charset.defaultCharset(), true);
                        FileUtils.write(new File("dir/file/filetihuan/" + path + "/" + path + "_onoss_norequest_nobaseline.txt"), "\r\n", Charset.defaultCharset(), true);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
        );


    }
}
